$(function() {
	$(".divLeft div img").click(function() {
		$(".divLeft div img").css("border", "2px #CCCCCC solid").attr("title", "");
		//去掉所有小图的高亮显示
		var path = $(this).css("border", "2px #FF4400 solid").attr("title", "小图片").attr("src").replace(".jpg", "_big.jpg");
		$(".divLeft img[title='商品图片']").attr("src", path);
	}).mouseover(function() {
		$(this).css("border", "2px #FF4400 solid");
	}).mouseout(function() {
		var name = $(this).attr("title");
		if (!name) {
			$(this).css("border", "2px #CCCCCC solid");
		}
	});
	getXml();
})

function getXml() {
	if (getCookie("browseTimes")) {
		//根据cookie中是否有数据来判断是不是第一次请求
		ajaxForGetXml(false);
	} else {
		ajaxForGetXml(true);
	}
}

function getCookie(name) {
	var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); //正则匹配
	if (arr = document.cookie.match(reg)) {
		//将cookie拆成一个数组,arr[0]是键,arr[1]是"：",arr[2]是值
		return unescape(arr[2]);
	} else {
		return null;
	}
}

function ajaxForGetXml(isFirst) {
	$.ajax({
		url: "file/Web.xml",
		dataType: 'xml',
		type: 'GET',
		timeout: 2000,
		error: function(xml) {
			alert("加载XML文件出错！");
		},
		success: function(xml) {
			var goodses = $(xml).find("goods");
			//获取所有商品信息
			var index;
			if (isFirst) {
				index = Math.floor(Math.random() * goodses.length);
				document.cookie = "index = " + index;
			} else {
				index = parseInt(getCookie("index"));
			}
			dataBind($(goodses[index]), isFirst);
		}
	});
}

function dataBind(goods, isFirst) {
	//把访问次数写入cookie
	var trObjs = $(".divRight tr");
	$(".bigDiv h4").text(goods.children("name").text());
	//更改标题
	$($(trObjs[0]).children("td")[1]).text(goods.children("price").text());
	//更改价钱【一口价】
	$($(trObjs[1]).children("td")[1]).text(goods.children("freight").text());
	//更改【运费】
	$($(trObjs[3]).children("td")[1]).text(goods.children("surplusTime").text()).css("color", "red");
	//更改【剩余天数】 第2行是图片
	$($(trObjs[4]).children("td")[1]).text(goods.children("thisTimeSell").text());
	//更改【本月售出】
	$($(trObjs[5]).children("td")[1]).text(goods.children("totalSell").text());
	//更改【累计售出】
	$($(trObjs[6]).children("td")[1]).text(goods.children("type").text());
	//更改【商品类型】
	$($(trObjs[6]).children("td")[3]).text(goods.children("location").text());
	//更改【所在地】
	$($(trObjs[7]).children("td")[1]).text(goods.children("surplusCount").text());
	//更改【剩余数量】
	var times;
	//获得访问次数
	if (isFirst) {
		times = goods.children("browseTimes").text();
		$($(trObjs[7]).children("td")[3]).text(times);
		//更改【浏览量】
		document.cookie = "browseTimes = " + times;
		//将访问次数保存到cookie中
	} else {
		//times = parseInt(times) + 1 + "次";
		times = parseInt(getCookie("browseTimes")) + 1 + "次";
		$($(trObjs[7]).children("td")[3]).text(times);
		//更改【浏览量】
		document.cookie = "browseTimes = " + times;
		//将访问次数保存到cookie中
	}
	countDown($($(trObjs[3]).children("td")[1]));
}

function countDown(time) {
	var mydate = new Date();
	var day = time.text();
	day = day.substring(0, day.indexOf("天"));
	//获得倒计时的天数
	var hour = 23 - mydate.getHours();
	//获得倒计时的时
	var min = 59 - mydate.getMinutes();
	//获得倒计时的分
	var second = 59 - mydate.getSeconds();
	//获得倒计时的秒
	time.text(day + "天" + hour + "点" + min + "分" + second + "秒");
	window.setTimeout(function() {
		countDown(time);
	}, 1000);
}
